<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="home" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="hAdd('')">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
      </el-card>
      <!-- props: 配置选项自定义标题类名 -->
      <!-- default-expand-all: 默认展开所有节点 -->
      <el-tree :data="list" default-expand-all>
        <template #default="{data}">
          <el-row type="flex" justify="space-between" align="middle" style="height: 40px;width:100%">
            <el-col :span="20">
              <svg-icon icon-class="drag" /><span>{{ data.name }}</span>
            </el-col>
            <el-col :span="4">
              <el-row type="flex" justify="end">
                <!-- 两个内容 -->
                <el-col>{{ data.manager }}</el-col>
                <!-- 下拉菜单 element -->
                <el-col>
                  <el-dropdown>
                    <span>
                      操作<i class="el-icon-arrow-down" />
                    </span>
                    <el-dropdown-menu slot="dropdown">
                      <el-dropdown-item @click.native="hAdd(data.id)">添加子部门</el-dropdown-item>
                      <el-dropdown-item @click.native="hEdit(data.id)">编辑部门</el-dropdown-item>
                      <el-dropdown-item v-if="data.children.length === 0" @click.native="hDel(data.id)">删除部门</el-dropdown-item>
                    </el-dropdown-menu>
                  </el-dropdown>
                </el-col>
              </el-row>
            </el-col>
          </el-row>
        </template>
      </el-tree>
      <el-dialog :title=" isEdit ? '编辑' : '添加'" :visible.sync="showDialog">
        <addOrEdit v-if="showDialog" :id="curId" :origin-list="originList" :is-edit="isEdit" @success="hSuccess" @close="showDialog=false" />
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getDepartment, deltetDepartmentsDetail } from '@/api/departments'
import { tranListTotreeList } from '@/utils'
import addOrEdit from './deptDialog.vue'
export default {
  components: {
    addOrEdit
  },
  data() {
    return {
      // 储存获取部门数据
      list: [],
      showDialog: false,
      curId: '', // 是否显示弹层
      isEdit: false,
      originList: []
    }
  },
  created() {
    this.laodDepartment()
  },
  methods: {
    // 删除部门
    hDel(id) {
      this.$confirm('此操作将永久删除该部门, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        // 确定
        try {
          await deltetDepartmentsDetail(id)
          // 重新加载页面
          this.laodDepartment()
        } catch (error) {
          console.log(error)
        }
      }).catch(() => {
        // 取消
      })
    },
    // 编辑部门
    hEdit(id) {
      console.log(777)
      this.showDialog = false
      this.curId = id
      this.isEdit = true
      // this.$nextTick(() => {
      //   this.$refs.deptDialog.loadgetDepartmentsDetail()
      // })
    },
    hSuccess() {
      // 关闭对话框
      this.showDialog = false
      // 刷新页面数据
      this.laodDepartment()
    },
    // 新增子部门
    hAdd(id) {
      this.showDialog = true
      this.curId = id
      this.isEdit = false
    },
    async laodDepartment() {
      // 发送请求获取部门数据
      try {
        const res = await getDepartment()
        console.log(res.data.depts, 'getDepartment')
        console.log(res.data.deots, 'res.data.depts')
        this.originList = res.data.depts.map(({ name, code, id, pid }) => ({
          name,
          code,
          id,
          pid
        }))
        // 将对象数组值第一个数据取出并返回剩下的数据放入一个新的数组中 shift()
        const company = res.data.depts.shift()
        console.log(company, 'company')
        // 将数据存入list数组中
        this.list = tranListTotreeList(res.data.depts)
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>

<style>

</style>
